home *** CD-ROM | disk | FTP | other *** search
/ NOVA - For the NeXT Workstation / NOVA - For the NeXT Workstation.iso / SourceCode / OOP_Course / Examples / DragDemo_1.1 / DiagramCell.m < prev    next >
Text File  |  1992-12-19  |  2KB  |  91 lines

  1. #import "DiagramCell.h"
  2.  
  3. /* DiagramCell - This is a simple graphic object to be used in the
  4. DemoDiagram view.  It owns an NXImage object which it composites into
  5. the diagram. */
  6.  
  7. @implementation DiagramCell:Object
  8.  
  9. - setImage:(NXImage *)anImage
  10. {
  11.     image=anImage;
  12.     frameRect.origin.x=frameRect.origin.y=0.0;
  13.     [image getSize:&frameRect.size];
  14.     NXIntegralRect(&frameRect);        //Insure no fractional dimensions
  15.     return self;
  16. }
  17.  
  18. - getFrame:(NXRect *)rect
  19. {
  20.     *rect=frameRect;
  21.     return self;
  22. }
  23.  
  24. /* In this method, this item has the responsibility for moving no
  25. further than up against the edge of the view rectangle it is passed. */
  26.  
  27. - moveBy:(NXCoord)dx :(NXCoord)dy inViewRect:(NXRect *)rect
  28. {
  29.     float newX,newY,minX,maxX,minY,maxY;
  30.     
  31.     /* Here is the desired new location */
  32.     newX = frameRect.origin.x + dx;
  33.     newY = frameRect.origin.y + dy;
  34.     
  35.     /* Here are the limits within the view rect.  Yes, this could
  36.     be calculated once in advance, perhaps in a setViewRect: method,
  37.     or in an initInRect: method. */
  38.     minX = rect->origin.x;
  39.     maxX = minX + rect->size.width - frameRect.size.width;
  40.     minY = rect->origin.y;
  41.     maxY = minY + rect->size.height - frameRect.size.height;
  42.     
  43.     /* Recalculate location constrained to the view rect. */
  44.     if(newX < minX)newX=minX;
  45.     if(newY < minY)newY=minY;
  46.     if(newX > maxX)newX=maxX;
  47.     if(newY > maxY)newY=maxY;
  48.     
  49.     /* Finally, "move" to the new location. */
  50.     frameRect.origin.x=newX;
  51.     frameRect.origin.y=newY;
  52.     return self;
  53. }
  54.  
  55. - mouseHit:(NXPoint *)location
  56. {
  57.     if(NXMouseInRect(location,&frameRect,NO)) return self;
  58.     else return nil;
  59. }
  60.  
  61. - drawSelf
  62. {
  63.     [image composite:NX_SOVER toPoint:&frameRect.origin];
  64.     return self;
  65. }
  66.  
  67. - drawInDirtyRect:(NXRect *)dirtyRect
  68. {
  69.     if(NXIntersectsRect(&frameRect,dirtyRect)){
  70.         NXPoint intersectPoint;                //origin of intersection in view coords
  71.         NXRect intersectRect=frameRect;
  72.  
  73.         /* Get intersection of this cell's frame with the dirty rect */
  74.         NXIntersectionRect(dirtyRect,&intersectRect);
  75.  
  76.         /* Save intersect origin in view's coordinates */
  77.         intersectPoint.x = intersectRect.origin.x;
  78.         intersectPoint.y = intersectRect.origin.y;
  79.  
  80.         /* Now convert intersection rect origin to cell image's coordinates */
  81.         intersectRect.origin.x  -=  frameRect.origin.x;
  82.         intersectRect.origin.y  -=  frameRect.origin.y;
  83.  
  84.         /* Composite from intersection in image to point in view */
  85.         [image composite:NX_SOVER fromRect:&intersectRect toPoint:&intersectPoint];
  86.     }
  87.     return self;
  88. }
  89.  
  90.  
  91. @end